﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.UserRegistration.Entity;
using Ivy.UserRegistration.DataAccess;

namespace Ivy.UserRegistration.DataAccess
{
   public class Sys_RoleMasterDao : IDaoInterface<Sys_RoleMaster, Sys_RoleMasterCriteria>
   {
       #region  Variable & constant
       #endregion  Variable & constant

       #region Constructor & Destructor
       public Sys_RoleMasterDao() : base() { }
       #endregion Constructor & Destructor

       #region Properties
       #endregion Properties

       #region Public Methods
       #region IDaoInterface<Sys_RoleMasterDao,Sys_RoleMasterCriteria> Members
       /// <summary>
       /// The is to insert a record in Sys_RoleMaster table
       /// </summary>
       /// <param name="Sys_RoleMasterEntity"></param>
       /// <returns></returns>
       public bool Insert(Sys_RoleMaster Sys_RoleMasterEntity)
       {
           bool IsOperationSuccessful = false;
           
               if (Sys_RoleMasterEntity.EntityState == Common.Framework.Base.EntityState.New)
               {
                   DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                   DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                   CmdObj.CommandText = "Ivy_Sys_RoleMaster_Insert";
                   //CmdObj.CommandText = UserManagementSQLCommandText.InsertUser();
                   CmdObj.CommandType = CommandType.StoredProcedure;
                   //CmdObj.CommandType = CommandType.Text;

                   DataAccessProviderUtility.AddParameter(CmdObj, "@Rolekey", ParameterDirection.Input, DbType.Int32, Sys_RoleMasterEntity.RoleKey);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@RoleCode", ParameterDirection.Input, DbType.String, Sys_RoleMasterEntity.RoleCode);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@RoleName", ParameterDirection.Input, DbType.String, Sys_RoleMasterEntity.RoleName);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@RoleDescription", ParameterDirection.Input, DbType.String, Sys_RoleMasterEntity.RoleDescription);
                   DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, Sys_RoleMasterEntity);

                   DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                   CmdObj.Connection = DbConn;
                   DbConn.Open();
                   CmdObj.Prepare();
                   int _NoOfRecords = CmdObj.ExecuteNonQuery();
                   if (_NoOfRecords == 1)
                   {
                       Sys_RoleMasterEntity.RoleKey = Convert.ToInt32(CmdObj.Parameters["@RoleKey"].Value);
                       Sys_RoleMasterEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                       Sys_RoleMasterEntity.Sys_DateOfModification = Sys_RoleMasterEntity.Sys_DateOfCreation;
                       Sys_RoleMasterEntity.EntityState = EntityState.UnChanged;
                       IsOperationSuccessful = true;
                   }
                   CmdObj.Dispose();
                   DbConn.Close();
               }
               else
               {
                   throw new Exception("For inserting new User entity data to database, [EntityState] attribute should be [New]");
               }
           
           return IsOperationSuccessful;
       }

       public bool Update(Sys_RoleMaster Sys_RoleMasterEntity)
       {
           bool IsOperationSuccessful = false;
           
               if (Sys_RoleMasterEntity.EntityState == Common.Framework.Base.EntityState.Changed)
               {
                   DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                   DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                   CmdObj.CommandText = "Ivy_Sys_RoleMaster_Update";
                   CmdObj.CommandType = CommandType.StoredProcedure;

                   DataAccessProviderUtility.AddParameter(CmdObj, "@RoleKey", ParameterDirection.Input, DbType.Int32, Sys_RoleMasterEntity.RoleKey);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@RoleCode", ParameterDirection.Input, DbType.String, Sys_RoleMasterEntity.RoleCode);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@RoleName", ParameterDirection.Input, DbType.String, Sys_RoleMasterEntity.RoleName);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@RoleDescription", ParameterDirection.Input, DbType.String, Sys_RoleMasterEntity.RoleDescription);
                   
                   DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, Sys_RoleMasterEntity);

                   DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                   CmdObj.Connection = DbConn;
                   DbConn.Open();
                   CmdObj.Prepare();
                   int _NoOfRecords = CmdObj.ExecuteNonQuery();
                   if (_NoOfRecords == 1)
                   {
                       Sys_RoleMasterEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                       Sys_RoleMasterEntity.EntityState = EntityState.UnChanged;
                       IsOperationSuccessful = true;
                   }

                   CmdObj.Dispose();
                   DbConn.Close();
               }
               else
               {
                   throw new Exception("For Updating existing Sys_RoleMaster entity data to database, [EntityState] attribute should be [Changed]");
               }
           
           return IsOperationSuccessful;
       }

       public bool Delete(Sys_RoleMaster Sys_RoleMasterEntity)
       {
           bool IsOperationSuccessful = false;
           
               Sys_RoleMasterEntity.EntityState = EntityState.Changed;
               Sys_RoleMasterEntity.Sys_IsDeleted = true;
               if (Update(Sys_RoleMasterEntity))
               {
                   IsOperationSuccessful = true;
               }

           return IsOperationSuccessful;
       }

       public List<Sys_RoleMaster> GetAll()
       {
           throw new NotImplementedException();
       }

       public List<Sys_RoleMaster> GetByCriteria(Sys_RoleMasterCriteria Sys_RoleMasterCriteria)
       {
           throw new NotImplementedException();
       }
       #endregion
       
       public Sys_RoleMaster GetByID(String ID)
       {
           throw new NotSupportedException();
       }

       
       public Sys_RoleMaster GetByRoleKey(int RoleKey)
       {
          Sys_RoleMaster _Sys_RoleMasterList = new Sys_RoleMaster(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.
           
               DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
               DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
               CmdObj.CommandText = "Ivy_Sys_RoleMaster_GetByID";
               CmdObj.CommandType = CommandType.StoredProcedure;

               DataAccessProviderUtility.AddParameter(CmdObj, "@RoleKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(RoleKey));
              

               CmdObj.Connection = DbConn;
               DbConn.Open();
               CmdObj.Prepare();
               DbDataReader _DataReader = CmdObj.ExecuteReader();

               if (_DataReader.HasRows)
               {
                   if (_DataReader.Read())
                   {
                       _Sys_RoleMasterList = new  Sys_RoleMaster(); //IMPORTANT NOTE:- Create instance only when required.
                       TranslateUserFieldsFromReaderToEntity(_Sys_RoleMasterList, _DataReader);
                   }
               }
               else
               {
                   throw new Exception("No Role found");
               }
               CmdObj.Dispose();
               DbConn.Close();
          
           return _Sys_RoleMasterList;
       }
       
       
       public Sys_RoleMaster GetByRoleCode(String RoleCode)
       {
           Sys_RoleMaster _Sys_RoleMasterList = new Sys_RoleMaster(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.
           
               DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
               DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
               CmdObj.CommandText = "Ivy_Sys_RoleMaster_GetByRoleCode";
               CmdObj.CommandType = CommandType.StoredProcedure;

               DataAccessProviderUtility.AddParameter(CmdObj, "@RoleCode", ParameterDirection.Input, DbType.String, Convert.ToString(RoleCode));


               CmdObj.Connection = DbConn;
               DbConn.Open();
               CmdObj.Prepare();
               DbDataReader _DataReader = CmdObj.ExecuteReader();

               if (_DataReader.HasRows)
               {
                   if (_DataReader.Read())
                   {
                       _Sys_RoleMasterList = new Sys_RoleMaster(); //IMPORTANT NOTE:- Create instance only when required.
                       TranslateUserFieldsFromReaderToEntity(_Sys_RoleMasterList, _DataReader);
                   }
               }
               else
               {
                   throw new Exception("No RoleCode found");
               }
               CmdObj.Dispose();
               DbConn.Close();
           
           return _Sys_RoleMasterList;
       }

       
       public Sys_RoleMaster GetByRoleName(String RoleName)
       {
           Sys_RoleMaster _Sys_RoleMasterList = new Sys_RoleMaster(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.
           
               DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
               DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
               CmdObj.CommandText = "Ivy_Sys_RoleMaster_GetByRoleName";
               CmdObj.CommandType = CommandType.StoredProcedure;

               DataAccessProviderUtility.AddParameter(CmdObj, "@RoleName", ParameterDirection.Input, DbType.String, Convert.ToString(RoleName));


               CmdObj.Connection = DbConn;
               DbConn.Open();
               CmdObj.Prepare();
               DbDataReader _DataReader = CmdObj.ExecuteReader();

               if (_DataReader.HasRows)
               {
                   if (_DataReader.Read())
                   {
                       _Sys_RoleMasterList = new Sys_RoleMaster(); //IMPORTANT NOTE:- Create instance only when required.
                       TranslateUserFieldsFromReaderToEntity(_Sys_RoleMasterList, _DataReader);
                   }
               }
               else
               {
                   throw new Exception("No RoleName found");
               }
               CmdObj.Dispose();
               DbConn.Close();
           
           return _Sys_RoleMasterList;
       }

       
       #endregion Public Methods

       #region Events
       #endregion Events

       #region Private Methods
       private void TranslateUserFieldsFromReaderToEntity(Sys_RoleMaster pSys_RoleMaster, DbDataReader pDbDataReader)
       {
             pSys_RoleMaster.RoleKey = Convert.ToInt32(pDbDataReader["RoleKey"]);
               pSys_RoleMaster.RoleCode = Convert.ToString(pDbDataReader["RoleCode"]);
               pSys_RoleMaster.RoleName = Convert.ToString(pDbDataReader["RoleName"]);
               pSys_RoleMaster.RoleDescription = Convert.ToString(pDbDataReader["RoleDescription"]);

               DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pSys_RoleMaster, pDbDataReader);
           
       }
       #endregion Private Methods

   }
}
